package com.egghiro.j2se.game.sace.model.actor.player;

import java.awt.event.KeyEvent;

import com.egghiro.framework.j2se.game.core.SGSystem;
import com.egghiro.framework.j2se.game.core.key.SKeyState;
import com.egghiro.framework.j2se.game.core.model.actor.Sprite;
import com.egghiro.framework.j2se.game.core.view.SImage;
import com.egghiro.framework.j2se.game.core.view.canvas.SGraphics;
import com.egghiro.framework.j2se.game.model.element.LinearVelocity2D;
import com.egghiro.framework.j2se.game.utils.PropertyUtils;
import com.egghiro.j2se.game.sace.model.actor.BulletCluster;
import com.egghiro.j2se.game.sace.model.actor.option.Option;
import com.egghiro.j2se.game.sace.model.actor.option.OptionComposite;
import com.egghiro.j2se.game.sace.model.actor.option.TaikyokuOption;
import com.egghiro.j2se.game.sace.param.Direction;
import com.egghiro.j2se.game.sace.param.SACEConst;

public class Fighter extends Sprite {
    private static final SImage IMG = SImage.createImage(PropertyUtils.getAIMPropertyValue(SACEConst.AIM_P_DIR,
            SACEConst.AIM_P_Key_IMG_FIGHTER));
    private static final int DEFAULT_SPEED = 10;
    private static final int MAX_OPTION_NUM = 4;
    private LinearVelocity2D speed;

    private FloatTail floatTail;

    private OptionComposite optionComposite;
    
    private BulletCluster canvasPlayerBulletCluster;
    
    private final String PLAYER_ID;

    public Fighter(BulletCluster canvasPlayerBulletCluster,final String PLAYER_ID) {
        super(IMG, IMG.getWidth(), IMG.getHeight());
        defineReferencePixel(getWidth() / 2, getHeight() / 2);

        floatTail = new FloatTail(this);
        speed = new LinearVelocity2D(0, 0);
        optionComposite = new OptionComposite();
        this.canvasPlayerBulletCluster = canvasPlayerBulletCluster;
        this.PLAYER_ID = PLAYER_ID;
    }

    @Override
    public void act() {
        this.checkKeyState();
        this.move(speed.getDx(), speed.getDy());
        floatTail.act(speed.getDx(), speed.getDy());

        optionComposite.act();
    }

    @Override
    public void paint(SGraphics g) {
        if (isVisible()) {
            // TODO 通過顯示狀態來繪畫
            // TODO TEST
            // g.save();
            // g.translate(getRefPixelX(), getRefPixelY());
            // g.drawImage(sImage.getBufferedImage(), -getRefX(),
            // -getRefY());
            // g.restore();

            optionComposite.paint(g);

            floatTail.paint(g);

            g.drawImage(sImage.getBufferedImage(), getX(), getY());
        }
    }

    @Override
    public void setPosition(int x, int y) {
        super.setPosition(x, y);
        floatTail.telport();
    }
    
    private void checkKeyState() {
        if (SKeyState.getSKeyState(PLAYER_ID).isKeyGameAPressed()) {
            fire();
        }
    }
    
    private void fire(){
        // TODO 自身的开火做成什么样还没想好。不过基础的就用普通发射器得了
        
        // Option
        optionComposite.fire();
    }

    public void addOption() {
        // TODO
        if (optionComposite.count() < MAX_OPTION_NUM) {
            // TODO
            optionComposite.add(new TaikyokuOption(this,this.canvasPlayerBulletCluster));
        }
    }

    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            speed.setAngleInDigree(Direction.UP.getValue());
            break;
        case KeyEvent.VK_DOWN:
            speed.setAngleInDigree(Direction.DOWN.getValue());
            break;
        case KeyEvent.VK_LEFT:
            speed.setAngleInDigree(Direction.LEFT.getValue());
            break;
        case KeyEvent.VK_RIGHT:
            speed.setAngleInDigree(Direction.RIGHT.getValue());
            break;
        default:
            break;
        }
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
        case KeyEvent.VK_DOWN:
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_RIGHT:
            speed.setSpeed(DEFAULT_SPEED);
            break;
        default:
            break;
        }
    }

    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
        case KeyEvent.VK_DOWN:
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_RIGHT:
            speed.setSpeed(0);
            break;
        default:
            break;
        }
    }

}
